<?php


namespace app\controller;

use app\model\AlbumPeople;
use app\model\AlbumTag;
use app\process\CalFeature;

/**
 * 相册
 * @package app\controller
 */
class AlbumPhoto extends BackendController{
    protected $model = null;
    protected $noNeedRight=['*'];
    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\model\AlbumPhoto();
    }

    public function ana_where($where){
        $in=[];
        foreach ($where as &$item){
            if($item[0]=='tags'){
                //$item=[['name',$item[1],'%'.$item[2].'%'],['name_code',$item[1],'%'.$item[2].'%'],'OR'];
                //$where[]=['name_code','LIKE','%'.$item[2].'%'];
                //$where['_logic']='or';
                $tags=explode(",",$item[2]);
                $ids=[];
                foreach ($tags as $tag) {
                    $temp_ids=$this->model->where('tags','find_in_set',$tag)->column('id');
                    $ids=array_merge($ids,$temp_ids);
                }
                if($ids){
                    $in=array_merge($in,$ids);
                }

            }/*elseif ($item[0]=='album_id'){
                $album_id=$item[2];
                $_people=new AlbumPeople();
                $ids=$_people->where('album_id',$album_id)->column('photo_id');
                if($ids){
                    $in=array_merge($in,$ids);
                }
            }*/
            if($in){
                $item=['id','in',$in];
            }
        }
        return $where;
    }

    public function after_index($lists) {
        if($lists){
            $_tag=new AlbumTag();
            $tags=$_tag->column('name','id');
            foreach ($lists as &$list) {
                if($list['tags']){
                    $all_tags=[];
                    $single_tags=explode(',',$list['tags']);
                    foreach ($single_tags as $single_tag){
                        $all_tags[]=$tags[$single_tag];
                    }
                    $list['tags_name']=implode(',',$all_tags);
                }

            }
        }
        return $lists;
    }

    public function edit_tags($id,$tags){
        $info=$this->model->find($id);
        if($info){
            $info->tags=$tags;
            $info->save();
            $this->success('修改成功');
        }else{
            $this->error('修改失败');
        }
    }

    public function show_cam($id){
        $info=$this->model->find($id);
        $_face=new \app\model\Faces();
        $data = $_face->where('photo_id', $info->people_id)
        ->field('count(*) as count, device_name, device_id') 
        ->group('device_id')
        ->order('count', 'desc')
        ->limit(3)
        ->select();
        $this->success('获取成功',$data);
    }

}